const postcss = require('postcss');
const path = require('path');

function replace(source) {
    return source.replace(/(\/\/ *@import) +(('|").+('|")).*/, 'require($2);');
}

function link(self, source) {
    const linkExp = /(?:<link\s+rel="stylesheet"\s+href="(.*)"\/>)/gim;
    return `module.exports = require(${JSON.stringify(linkExp.exec(source)[1]) })`;
}


function style(self, source) {
    // const styleExp = /(?:<style\s+(scss|sass|less|pcss)?>(?:[\s\t\n]*)*(.*)+(?:[\s\t\n]*)*<\/style>)/igm;
    let content = source.replace(/[ | ]*\n/igm, '\n').replace(/\n[\s| | ]*\r/igm, '\n').replace(/ /igm, '').replace(/^[\s　]+|[\s　]+$/igm, '').replace(/[\r\n]/igm, '');
    console.log(content);
    console.log(1111111111111);
    let styleExp = /<style(scss|sass|less|pcss)?>(.*)<\/style>/igm;
    let input = styleExp.exec(content);
    console.log('--------->style');
    // switch (content[1].toLowerCase()) {
    //     case 'scss':
    // }
    console.log();
    // console.log(input);
    //
    console.log(postcss.plugin(''))

    // return content[1] != null ? `;module.exports = require(${JSON.stringify(content[2]) })` : `module.exports = require(${JSON.stringify(content[2]) })`;
}

module.exports = function (self, source) {
    const cssExp = /(link|style)/;
    if (cssExp.test(source)) {
        let type = cssExp.exec(source)[1];
        console.log('----->');
        console.log(type);
        return type === 'link' ? link(self, source) : style(self, source);
    }
    return null;
};
